//
//  JSHeaebyNaviView.m
//  NiubilityProject
//
//  Created by Apple on 2017/12/4.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "JSHeaebyNaviView.h"
@interface JSHeaebyNaviView ()

@property (nonatomic, strong) UIView *line;
@property (nonatomic, weak) UIButton *nearbyBtn;
@property (nonatomic, weak) UIButton *meetBtn;


@end
@implementation JSHeaebyNaviView

- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles{
    if (self = [super initWithFrame:frame]) {
        _isNearby = YES;
        UIButton *nearbyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.nearbyBtn = nearbyBtn;
        [nearbyBtn setTitle:titles.firstObject forState:UIControlStateNormal];
        [nearbyBtn setTitleColor:JS_colorWithHex(0x333333) forState:UIControlStateNormal];
        [nearbyBtn sizeToFit];
        nearbyBtn.titleLabel.font = UIFontMake(15);
        [self addSubview:nearbyBtn];
        nearbyBtn.top = 0;
        nearbyBtn.left = 0;
        [nearbyBtn addTarget:self action:@selector(nearbyBtnClick) forControlEvents:UIControlEventTouchUpInside];
        UIButton *meetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [meetBtn addTarget:self action:@selector(meetBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        self.meetBtn = meetBtn;
        [meetBtn setTitle:titles.lastObject forState:UIControlStateNormal];
        [meetBtn setTitleColor:JS_colorWithHex(0x333333) forState:UIControlStateNormal];
        [meetBtn sizeToFit];
        meetBtn.titleLabel.font = UIFontMake(15);
        [self addSubview:meetBtn];
        [meetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.and.right.equalTo(self);
        }];
        _line = [UIView new];
        _line.backgroundColor = JS_colorWithHex(kBodyColor);
        [self addSubview:_line];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        _isNearby = YES;
        UIButton *nearbyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.nearbyBtn = nearbyBtn;
        [nearbyBtn setTitle:JSLocalizedString(@"homeNearby") forState:UIControlStateNormal];
        [nearbyBtn setTitleColor:JS_colorWithHex(0x333333) forState:UIControlStateNormal];
        [nearbyBtn sizeToFit];
        nearbyBtn.titleLabel.font = UIFontMake(15);
        [self addSubview:nearbyBtn];
        nearbyBtn.top = 0;
        nearbyBtn.left = 0;
        [nearbyBtn addTarget:self action:@selector(nearbyBtnClick) forControlEvents:UIControlEventTouchUpInside];
        UIButton *meetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [meetBtn addTarget:self action:@selector(meetBtnClick) forControlEvents:UIControlEventTouchUpInside];

        self.meetBtn = meetBtn;
        [meetBtn setTitle:JSLocalizedString(@"homeMeet") forState:UIControlStateNormal];
        [meetBtn setTitleColor:JS_colorWithHex(0x333333) forState:UIControlStateNormal];
        [meetBtn sizeToFit];
        meetBtn.titleLabel.font = UIFontMake(15);
        [self addSubview:meetBtn];
        [meetBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.and.right.equalTo(self);
        }];
        _line = [UIView new];
        _line.backgroundColor = JS_colorWithHex(kBodyColor);
        [self addSubview:_line];
//        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
//
//        }];
    }
    return self;
}
-(void)nearbyBtnClick{
    if (self.isNearby) {
        return;
    }
    self.isNearby = YES;
    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
        [UIView animateWithDuration:0.3 animations:^{
            [self layoutIfNeeded];
        }];
    
}
-(void)meetBtnClick{
    if (!self.isNearby) {
        return;
    }
    self.isNearby = NO;
    [self setNeedsUpdateConstraints];
    [self updateConstraintsIfNeeded];
        [UIView animateWithDuration:0.3 animations:^{
            [self layoutIfNeeded];
        }];
}
- (void)updateConstraints{
    if (self.isNearby) {
        [self.line mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self.nearbyBtn);
            make.top.equalTo(self.nearbyBtn.mas_bottom);
            make.height.mas_equalTo(2);
            make.centerX.equalTo(self.nearbyBtn);
        }];
    }else{
        [self.line mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self.meetBtn);
            make.top.equalTo(self.meetBtn.mas_bottom);
            make.height.mas_equalTo(2);
            make.centerX.equalTo(self.meetBtn);
        }];
    }
    [super updateConstraints];

}
- (CGSize)intrinsicContentSize{
    return CGSizeMake(SCREEN_WIDTH *0.5, 40);
}
@end
